package cn.nexd.map.algorithm;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by admin on 15/8/17.
 */

public class LocationCore {

    private NB_Classifier clf;
    private LocationAlgorithm algo;
    private String DEFAULTFLOOR;

    public LocationCore(){
            DEFAULTFLOOR = "101001040002";// Beijing APM Floor 1
            algo = new LocationAlgorithm();
    }

//    public String locatePosition(String wifi_input, String floor_id) throws JSONException, IOException {
//        if (this.DEFAULTFLOOR.equals(floor_id)) {
//            //Do Nothing
//        }
//        else{
//            this.DEFAULTFLOOR = floor_id;
//            loadConfiguration();
//        }
//        try {
//            String res = _output_json(_locate(_parse_input(wifi_input), floor_id));
//            System.out.println(res);
//            return res;
//        } catch (NullPointerException e) {
//            return _output_json(new Location_Result("", "", ""));
//        }
//    }

//    private void loadConfiguration() throws IOException, JSONException {
//        Log.d("onConfLoading","Load Floor  " + this.DEFAULTFLOOR);
//        ConfLoader cl = new ConfLoader();
//        this.clf = cl.Load(this.DEFAULTFLOOR);
//    }
//
//    public void loadConfiguration_input(InputStream inputStream) throws IOException, JSONException {
//        ConfLoader cl = new ConfLoader();
//        this.clf = cl.Load_Stream(inputStream);
//    }

    public void showClassifier(){
        for(String wifi: this.clf.wifi_list){
            System.out.println(wifi);
        }
    }

    private Location_Result _locate(Double[] input, String floor_id){
        // Call Other Functions
//        System.out.println(input);
        Location_Result res =  algo.locateCoordinate(input, this.clf);
        res.setfloor_id(floor_id);
        return res;
    }

    private Double[] _parse_input(String wifi_input) throws JSONException {
        JsonParseUtil json_parser = new JsonParseUtil();
        Map<String, Object> json_res = json_parser.parse(wifi_input);
        Double[] test = new Double[this.clf.wifi_list.length];

        for(int i=0;i<test.length;i++){
            test[i] = 0.0;
        }
        for(String key: json_res.keySet()){
            int index = _search_in_array(this.clf.wifi_list, key);
            if(index != -1) {
                test[index] = Double.parseDouble((String) json_res.get(key));
            }
        }
        return test;
    }

    private int _search_in_array(String[] list, String target){
        int ind = -1;
        for(int i = 0; i < list.length; i++){
            if (list[i].equals(target)){
                ind = i;
                return ind;
            }
        }
        return ind;
    }


    private String _output_json(Location_Result res) throws JSONException {
        JSONObject obj = new JSONObject();
        obj.put("floor_id",res.floor_id);
//        obj.put("x",res.x);
//        obj.put("y",res.y);
        obj.put("x","167.0");
        obj.put("y","88.0");
        return obj.toString();
    }

}
